<?php
class wisatas extends ci_controller
{

	function __construct(){
		
		parent::__construct();
		$this->load->library('tank_auth');
		$this->load->library('bara_lib');
		$this->load->model('user');
		$this->load->model('admin/wisata');
		$this->load->model('admin/page');
		$this->load->model('admin/notification');

		if (!$this->tank_auth->is_logged_in()) redirect('/auth/login/');
		$this->load->helper('bara_helper');
		$this->lang->load('tank_auth');
		
	}

	function index(){

		//check_permission
		$this->bara_lib->check_permission_show($this->tank_auth->get_user_id(), 'wisata');
		//end

		/*$order = 'id_news';
		$by = 'desc';*/
		/*$where = array(
			'id_language' => 1,
			);*/
	/*	$order = 'id_news_language';
		$by = 'asc';
		$data['rows'] = $this->page->list_news($order, $by);*/
		$data['content'] = 'admin/wisata/index';
		$this->load->view('layout/backend', $data);
	}

	function pagination(){
		if ($_GET['page']) {
			$page = $_GET['page'];

			$data['cur_page'] = $page;
			$cur_page = $page;
			$page -= 1;
			$per_page = 10;
			$data['per_page'] = $per_page;
			$start = $page * $per_page;
			$query = $this->wisata->data_pagination($start, $per_page);

			$count = $this->wisata->pagination();
			$data['no_of_paginations'] = ceil($count / $per_page);

			$data['rows'] = $query;
			$data['count'] = count($query);
			$this->load->view('admin/wisata/pagination', $data);
		}
	}
	function show(){

		//check_permission
		$this->bara_lib->check_permission_show($this->tank_auth->get_user_id(), 'wisata');
		//end

		$id_wisata = $this->uri->segment(4);

		$where = array('id_wisata' => $id_wisata);
		$data['wisata'] = $this->user->edit('wisata', $where);
		$data['wisata_language'] = $this->user->edit('wisata_language', $where);
		
		$data['content'] = 'admin/wisata/show';
		$this->load->view('layout/backend', $data);
	}

	function add(){
		
		//check_permission
		$row = $this->bara_lib->check_permission_create($this->tank_auth->get_user_id(), 'wisata');
		//end
		$data['content'] = 'admin/wisata/add';
		$this->load->view('layout/backend', $data);
	}

	function edit(){
		//check_permission
		$this->bara_lib->check_permission_update($this->tank_auth->get_user_id(), 'wisata');
		//end

		$id_wisata = $this->uri->segment(4);

		$where = array('id_wisata' => $id_wisata);
		$data['wisata'] = $this->user->edit('wisata', $where);
		$data['wisata_language'] = $this->user->edit('wisata_language', $where);
		
		$data['content'] = 'admin/wisata/edit';
		$this->load->view('layout/backend', $data);
	}

	function create(){

		//check_permission
		$this->bara_lib->check_permission_create($this->tank_auth->get_user_id(), 'wisata');
		//end

		//validation
		//$this->form_validation->set_rules('project_date', 'Project date', 'trim|required|xss_clean');
		// $this->form_validation->set_rules('end_date', 'End date', 'trim|required|xss_clean');
		$this->form_validation->set_rules('judul_wisata_id', 'Judul wisata Id', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('content_wisata_id', 'Content wisata Id', 'trim|required|min_length[5]|xss_clean');
		// $this->form_validation->set_rules('judul_news_en', 'judul news En', 'trim|required|min_length[3]|xss_clean');
		// $this->form_validation->set_rules('content_news_en', 'Content news En', 'trim|required|min_length[5]|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$data['content'] = 'admin/wisata/add';
			$this->load->view('layout/backend', $data);
		}else{
			$date = get_date_time();

			$config['remove_spaces']  = TRUE;
			$config['upload_path'] = './uploads/wisata/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';

			$this->load->library('upload', $config);
			
			$this->upload->do_upload();

			$datetime = strtotime(get_date_time());

			$file = $_FILES["userfile"]["name"];
			$data = array('upload_data' => $this->upload->data());
			$name = $this->input->post('name');

			
			$file = str_replace(' ', '_', $file);

			$data_wisata = array(
				'url_gambar_wisata' => "uploads/wisata/".$datetime."_".$file,
				'latitude_wisata' => $this->input->post('latitude_wisata'),
				'longitude_wisata' => $this->input->post('longitude_wisata'),
				'id_member' => $this->input->post('id_member'),
				'id_kota' => $this->input->post('id_kota'),
				'id_provinsi' => $this->input->post('id_provinsi'),
				'date_update' => $date,
				'id_kategori_sub' => $this->input->post('id_kategori_sub'),
				);
			
			$base = base_url();

			
			$url = $this->bara_lib->url_dir();

			rename($url.'uploads/wisata/'.$file, $url.'uploads/wisata/'.$datetime."_".$file);


			// if(get_date() == $this->input->post('start_date')){
			// 	$status = array('status' => '1');
			// 	$data_news = array_merge($data_news, $status);
			// }


			$id_wisata = $this->user->insert_get('wisata', $data_wisata);

			$data_wisata_language_id = array(
				'judul_wisata' => $this->input->post('judul_wisata_id'),
				'content_wisata' => $this->input->post('content_wisata_id'),
				'harga_tiket_wisata' => $this->input->post('harga_tiket_wisata_id'),
				'alamat_wisata' => $this->input->post('alamat_wisata_id'),
				'kontak_wisata' => $this->input->post('kontak_wisata_id'),
				'id_wisata' => $id_wisata,
				'id_language' => 1,
				);

			$this->user->insert('wisata_language', $data_wisata_language_id);
			
			redirect('admin/wisatas/index');
		}

		
	}

	function update(){
		//check_permission
		$this->bara_lib->check_permission_update($this->tank_auth->get_user_id(), 'wisata');
		//end

		//validation
		$this->form_validation->set_rules('project_date', 'Project date', 'trim|required|xss_clean');
		// $this->form_validation->set_rules('end_date', 'End date', 'trim|required|xss_clean');
		$this->form_validation->set_rules('judul_wisata_id', 'Judul wisata Id', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('content_wisata_id', 'Content wisata Id', 'trim|required|min_length[5]|xss_clean');
		// $this->form_validation->set_rules('judul_news_en', 'judul news En', 'trim|required|min_length[3]|xss_clean');
		// $this->form_validation->set_rules('content_news_en', 'Content news En', 'trim|required|min_length[5]|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$id_wisata = $this->input->post('id_wisata');

			$where = array('id_wisata' => $id_wisata);
			$data['wisata'] = $this->user->edit('wisata', $where);
			$data['wisata_language'] = $this->user->edit('wisata_language', $where);	
			$data['content'] = 'admin/wisata/edit';
			$this->load->view('layout/backend', $data);
		}else{

			$date = get_date_time();
			$config['remove_spaces']  = TRUE;
			$config['upload_path'] = './uploads/wisata/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';

			$this->load->library('upload', $config);

			if ($this->upload->do_upload()) {
				$data = array('upload_data' => $this->upload->data());
				$file = $_FILES["userfile"]["name"];
				$name = $this->input->post('name');


				$base = base_url();

				$path_to_file = $this->bara_lib->url_dir().$this->input->post('gambar');
				

				unlink($path_to_file);
				$file = str_replace(' ', '_', $file);

				$datetime = strtotime(get_date_time());
				$data_wisata = array(
					'url_gambar_wisata' => "uploads/wisata/".$datetime."_".$file,
					'date_update' => $date,
					'latitude_wisata' => $this->input->post('latitude_wisata'),
					'longitude_wisata' => $this->input->post('longitude_wisata'),
					'id_member' => $this->input->post('id_member'),
					'id_kota' => $this->input->post('id_kota'),
					'id_provinsi' => $this->input->post('id_provinsi'),
					'id_kategori_sub' => $this->input->post('id_kategori_sub'),
					);

				$url = $this->bara_lib->url_dir();

				rename($url.'uploads/wisata/'.$file, $url.'uploads/wisata/'.$datetime."_".$file);
			}else{
				$data_wisata = array(
					'date_update' => $date,
					//'date_project' => $this->input->post('project_date'),
					'latitude_wisata' => $this->input->post('latitude_wisata'),
					'longitude_wisata' => $this->input->post('longitude_wisata'),
					'id_member' => $this->input->post('id_member'),
					'id_kota' => $this->input->post('id_kota'),
					'id_provinsi' => $this->input->post('id_provinsi'),
					'id_kategori_sub' => $this->input->post('id_kategori_sub'),
					);
			}

			$where_wisata = array('id_wisata' => $this->input->post('id_wisata'));
			$this->user->update('wisata',$where_wisata, $data_wisata);

			$where_wisata_l_id = array('id_wisata_language', $this->input->post('id_wisata_language_id'));
			$data_wisata_language_id = array(
				'judul_wisata' => $this->input->post('judul_wisata_id'),
				'content_wisata' => $this->input->post('content_wisata_id'),
				'harga_tiket_wisata' => $this->input->post('harga_tiket_wisata_id'),
				'alamat_wisata' => $this->input->post('alamat_wisata_id'),
				'kontak_wisata' => $this->input->post('kontak_wisata_id'),
				'id_language' => 1,
				);


			$where_wisata_l_id = array('id_wisata_language' => $this->input->post('id_wisata_language_id'));

			$this->user->update('wisata_language', $where_wisata_l_id, $data_wisata_language_id);


			redirect('admin/wisatas/index');
		}

	}

	function delete(){
		//check_permission
		$this->bara_lib->check_permission_delete($this->tank_auth->get_user_id(), 'wisata');
		//end

		$id_wisata = $this->uri->segment(4);

		$where = array('id_wisata' => $id_wisata);
		$rows = $this->user->edit('wisata', $where);

		foreach($rows as $row){
			$base = base_url();

			$path_to_file = $this->bara_lib->url_dir().$row->url_gambar_wisata;
	
			unlink($path_to_file); 
		}

		$where = array('id_wisata' => $id_wisata);
		$this->user->delete('wisata', $where);
		$this->user->delete('wisata_language', $where);
		
		redirect('admin/wisatas/index');
	}

	


	}
?>